package v1

import (
	"bc/service"
	"bc/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

type department struct {

}
func (t *department)Create(c *gin.Context){

	m := make(map[string]interface{})

	if v,ok := c.Get("uid");ok{
		m["uid"] = v
	}
	if v,ok := c.GetPostForm("f_dpid");ok{
		m["f_dpid"] = v
	}

	v,ok := c.GetPostForm("name")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请输入部门名称",
		})
		return
	}
	m["name"] = v

	if v,ok := c.GetPostForm("desc");ok{
		m["desc"] = v
	}
	m["uid"] = c.GetInt("uid")
	w := map[string]interface{}{}
	w["name"] = m["name"]

	b,_ := service.Dep.First(w)
	if b {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"已有该部门，请修改名称",
		})
		return
	}
	bool,last_id := service.Dep.Create(m)
	if !bool {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"错误，请重试",
		})
		return
	}
	LogAdd(c,"添加部门" + strconv.Itoa(last_id), "部门管理")
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":"添加成功",
	})

}
func (t *department)Delete(c *gin.Context){
	dp_id := c.Param("dp_id")
	if dp_id == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请选择",
		})
		return
	}
	dp_id_int,err := strconv.Atoi(dp_id)
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"错误，请重试",
		})
		return
	}
	bool := service.Dep.Delete(dp_id_int)
	code := 200
	msg := "操作成功"
	data := "成功"
	if !bool {
		code = 500
		msg = "error"
		data = "失败"
	}
	LogAdd(c,"删除部门" + dp_id, "部门管理")
	c.JSON(200,gin.H{
		"code":code,
		"msg":msg,
		"data":data,
	})
}
func (t *department)Update(c *gin.Context){

	dp_id := c.Param("dp_id")
	if dp_id == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请选择",
		})
		return
	}
	w := map[string]interface{}{
		"dp_id":dp_id,
	}

	d := map[string]interface{}{}

	if v,ok := c.GetPostForm("f_dpid");ok{
		d["f_dpid"] = v
	}
	if v,ok := c.GetPostForm("name");ok{
		d["name"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		d["desc"] = v
	}
	b := service.Dep.Update(w,d)
	if b {
		LogAdd(c,"修改部门" + dp_id, "部门管理")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"失败",
	})

}
func (t *department)Find(c *gin.Context){

	m := map[string]interface{}{}

	if v,ok := c.GetPostForm("f_dpid");ok{
		m["f_dpid"] = v
	}
	if v,ok := c.GetPostForm("name");ok{
		m["name"] = v
	}

	page,_ := strconv.Atoi(c.DefaultQuery("page","1"))
	pageSize := c.DefaultQuery("pageSize",strconv.FormatInt(util.Config.App.PageSize,10) )
	NpageSize,_ := strconv.Atoi(pageSize)

	d,total,e := service.Dep.Find(m,page,NpageSize)

	if e != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",

		})
		fmt.Println(e)
		return
	}

	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":d,
		"total":total,
	})
}
func (t *department)First(c *gin.Context){

	w := map[string]interface{}{}
	if v,ok := c.GetPostForm("f_dpid");ok{
		w["f_dpid"] = v
	}
	if v,ok := c.GetPostForm("name");ok{
		w["name"] = v
	}
	b,data := service.Dep.First(w)
	if b {
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":data,
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"请重试",
	})

}
var Dep = &department{}
